package top.jandmla.excel.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import top.jandmla.excel.bean.Menu;
import top.jandmla.excel.bean.Role;
import top.jandmla.excel.bean.RoleMenu;
import top.jandmla.excel.dao.MenusMapper;
import top.jandmla.excel.dao.RoleMapper;
import top.jandmla.excel.dao.RoleMenuMapper;
import top.jandmla.excel.service.RoleMenuService;
import top.jandmla.excel.util.result.BaseResult;

import javax.annotation.Resource;

@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {

    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private MenusMapper menusMapper;


    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> insertRoleAndMenu(RoleMenu roleMenu) {

        //判断参数
        if (roleMenu == null || StringUtils.isEmpty(roleMenu.getRoleId()) || StringUtils.isEmpty(roleMenu.getMenuId()))
            return BaseResult.fail(1,"添加失败，非法参数传输异常！");

        //判断角色
        QueryWrapper<Role> qwr = new QueryWrapper<>();
        qwr.eq("id",roleMenu.getRoleId());
        if (roleMapper.selectCount(qwr) < 1)
            return BaseResult.fail(1,"ID为："+roleMenu.getRoleId()+" 的角色不存在!");

        //判断权限
         QueryWrapper<Menu> qwm = new QueryWrapper<>();
         qwm.eq("id",roleMenu.getMenuId());
         if (menusMapper.selectCount(qwm) < 1)
             return BaseResult.fail(1,"ID为："+roleMenu.getMenuId()+" 的权限不存在！");

         //判断角色权限
        QueryWrapper<RoleMenu> qwrm = new QueryWrapper<>();
        qwrm.eq("role_id",roleMenu.getRoleId());
        qwrm.eq("menu_id",roleMenu.getMenuId());
        if (roleMenuMapper.selectCount(qwrm) > 0)
            return BaseResult.fail(1,"该角色已经拥有该权限，请勿重新赋权！");

        //赋权
        return roleMenuMapper.insert(roleMenu) > 0 ? BaseResult.success("赋权成功！") : BaseResult.fail(1,"赋权失败，发生未知错误！");
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
    public BaseResult<String> deleteRoleAndMenu(RoleMenu roleMenu) {
        if (roleMenu == null || StringUtils.isEmpty(roleMenu.getRoleId()) || StringUtils.isEmpty(roleMenu.getMenuId()))
            return BaseResult.fail(1,"添加失败，非法参数传输异常！");

        //判断角色权限
        QueryWrapper<RoleMenu> qwrm = new QueryWrapper<>();
        qwrm.eq("role_id",roleMenu.getRoleId());
        qwrm.eq("menu_id",roleMenu.getMenuId());
        if (roleMenuMapper.selectCount(qwrm)  < 1)
            return BaseResult.fail(1,"删除失败，该角色没有该权限！");
        return roleMenuMapper.delete(qwrm) > 0 ? BaseResult.success("删除成功！") : BaseResult.fail(1,"删除失败，发生未知错误！");
    }
}
